//
//  Home1Card1TableViewCell.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/19.
//  
  

#import "Home1Card1TableViewCell.h"
#import <YYKit.h>
#import <Masonry.h>
#import <YYKit.h>

@interface CapsuleTextView ()
@property (nonatomic, strong) UIView *textBackground;
@end
@implementation CapsuleTextView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        self.layer.borderWidth = 1.5;
        self.backgroundColor = UIColor.whiteColor;
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.textBackground];
        [self addSubview:self.textLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).inset(12);
            make.top.bottom.equalTo(self);
        }];
        [self.textBackground mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel.mas_right).offset(6);
            make.top.bottom.equalTo(self).inset(5);
            make.right.equalTo(self).inset(5);
        }];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.textBackground).inset(13);
            make.top.bottom.equalTo(self.textBackground);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = CGRectGetHeight(self.bounds)*0.5;
    self.textBackground.layer.cornerRadius = CGRectGetHeight(_textBackground.bounds)*0.5;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = UIColorHex(#666666);
        _titleLabel.font = aspectDefaultFont(14);
        _titleLabel.text = @"Loan tenure:";
    }
    return _titleLabel;
}
- (UIView *)textBackground {
    if (!_textBackground) {
        _textBackground = [UIView new];
        _textBackground.backgroundColor = UIColorHex(#FDC5E0);
    }
    return _textBackground;
}
- (UILabel *)textLabel {
    if (!_textLabel) {
        _textLabel = [UILabel new];
        _textLabel.textColor = UIColorHex(#1B1B1B);
        _textLabel.font = [UIFont systemFontOfSize:aspectValue(14) weight:700];
        _textLabel.text = @"91-180days";
    }
    return _textLabel;
}
@end



@implementation Home1Card1TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        [self setupValue];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self setupValue];
    }
    return self;
}

- (void)setupUI {
    [self.contentView addSubview:self.bgView];
    [self.contentView addSubview:self.circleLine];
    [self.contentView addSubview:self.bgimageView];
    [self.contentView addSubview:self.moneyTipLabel];
    [self.contentView addSubview:self.moneyLabel];
    [self.contentView addSubview:self.loanTenureLabel];
    [self.contentView addSubview:self.interestRateLabel];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView).inset(20);
        make.top.equalTo(self.contentView).inset(20);
        make.bottom.equalTo(self.contentView).inset(16);
    }];
    [self.circleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.bgView);
    }];
    [self.bgimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).inset(10);
        make.bottom.equalTo(self.bgView).inset(23);
        make.width.height.mas_equalTo(100);
    }];
    [self.moneyTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(21);
        make.top.equalTo(self.bgView).offset(46);
        make.height.mas_equalTo(26);
    }];
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(21);
        make.top.equalTo(self.bgView).offset(69);
        make.height.mas_equalTo(54);
    }];
    [self.loanTenureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(14);
        make.top.equalTo(self.moneyLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(35);
    }];
    [self.interestRateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(14);
        make.top.equalTo(self.loanTenureLabel.mas_bottom).offset(8);
        make.height.mas_equalTo(35);
        make.bottom.equalTo(self.bgView).inset(29);
    }];
}

- (void)setupValue {
    self.loanTenureLabel.titleLabel.text = @"Loan tenure:";
    self.loanTenureLabel.textLabel.text = @"91-180days";
    
    self.interestRateLabel.titleLabel.text = @"Interest rate:";
    self.interestRateLabel.textLabel.text = @"0.06%";
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}
- (UIImageView *)circleLine {
    if (!_circleLine) {
        _circleLine = [UIImageView new];
        _circleLine.contentMode = UIViewContentModeScaleAspectFit;
        _circleLine.image = [UIImage imageNamed:@"home_card"];
    }
    return _circleLine;
}
- (UIImageView *)bgimageView {
    if (!_bgimageView) {
        _bgimageView = [UIImageView new];
        _bgimageView.contentMode = UIViewContentModeScaleAspectFit;
        _bgimageView.backgroundColor = UIColor.redColor;
        _bgimageView.hidden = YES;
    }
    return _bgimageView;
}
- (UILabel *)moneyTipLabel {
    if (!_moneyTipLabel) {
        _moneyTipLabel = [UILabel new];
        _moneyTipLabel.textColor = UIColorHex(#666666);
        _moneyTipLabel.font = aspectDefaultFont(18);
        _moneyTipLabel.text = @"Maximum loanable (₱)";
    }
    return _moneyTipLabel;
}
- (UILabel *)moneyLabel {
    if (!_moneyLabel) {
        _moneyLabel = [UILabel new];
        _moneyLabel.textColor = UIColorHex(#333333);
        _moneyLabel.font = [UIFont systemFontOfSize:aspectValue(50) weight:700];
        _moneyLabel.text = @"50,000";
    }
    return _moneyLabel;
}
- (CapsuleTextView *)loanTenureLabel {
    if (!_loanTenureLabel) {
        _loanTenureLabel = [CapsuleTextView new];
    }
    return _loanTenureLabel;
}
- (CapsuleTextView *)interestRateLabel {
    if (!_interestRateLabel) {
        _interestRateLabel = [CapsuleTextView new];
    }
    return _interestRateLabel;
}
@end
